# coding=utf-8
import cgi
import wsgiref.handlers
import datetime
from google.appengine.ext import db
from xml.dom import minidom
from google.appengine.ext import webapp
from google.appengine.api import urlfetch

class GetXml(webapp.RequestHandler):
    def parse_t(self,url):
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            content=result.content
        return minidom.parseString(content)

class Twetts(db.Model):
    twitter_user = db.StringProperty()
    since_id = db.StringProperty()
    max_id = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)

twee=None
def InitTwettsData():
    global twee
    twee = Twetts(key_name = 'default')
    twee.twitter_user = 'haitai' #replace it with your twitter account
    n='https://twitter.com/statuses/user_timeline/%s.xml?count=1' %(twee.twitter_user)
    s_id = GetXml().parse_t(n).getElementsByTagName('status')[0].getElementsByTagName('id')[0].firstChild.nodeValue
    twee.since_id = s_id
    twee.max_id = s_id
    twee.save()

def twee_init():
    global twee
    twee = Twetts.get_by_key_name('default')
    if not twee:
        twee=InitTwettsData()
twee_init()
